package main

import (
	"context"
	"flag"
	"log"

	tg_client "example.com/read-adviser-bot/clients/telegram"
	event_consumer "example.com/read-adviser-bot/consumer/event-consumer"
	"example.com/read-adviser-bot/events/telegram"
	"example.com/read-adviser-bot/storage/sqlite"
)

// 7049302372:AAHzXZHSCNK05UMPFOxDD7H5mgD6y2xwThg
// tgBotHost   = "api.telegram.org"
const (
	tgBotHost       = "api.telegram.org"
	storageFilePath = "file_storage"
	storageSQLPath  = "data/sqlite/storage.db"
	batchSize       = 100
)

func main() {
	// s := files.New(storageFilePath)
	s, err := sqlite.New(storageSQLPath)
	if err != nil {
		log.Fatal("can't connect to storage: ", err)
	}

	// TODO: Set timeout to context
	// context.WithTimeout(context.Background(), 10*time.Second)
	err = s.Init(context.TODO())
	if err != nil {
		log.Fatal("can't init storage: ", err)
	}

	events_processor := telegram.New(
		tg_client.New(tgBotHost, mustToken()),
		s,
	)

	log.Print("service started")

	consumer := event_consumer.New(events_processor, events_processor, batchSize)
	if err := consumer.Start(); err != nil {
		log.Fatal("Service is stopped", err)
	}
}

func mustToken() string {
	token := flag.String("tg-bot-token", "", "token for access to telegram bot")
	flag.Parse()
	if *token == "" {
		log.Fatal("token is not specified")
	}
	return *token
}

// func mustTgBotHost() string {
// 	host := flag.String("tg-bot-host", "", "host for access to telegram bot")
// 	flag.Parse()
// 	if *host == "" {
// 		log.Fatal("host is not specified")
// 	}
// 	return *host
// }
